package mdm.com.sap;

import java.util.Properties;

import org.apache.log4j.Logger;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import com.sap.conn.jco.JCoDestination;
import com.sap.conn.jco.JCoDestinationManager;
import com.sap.conn.jco.JCoException;
import com.sap.conn.jco.JCoFunction;
import com.sap.conn.jco.JCoParameterList;
import com.sap.conn.jco.ext.DestinationDataProvider;
import com.sap.conn.jco.ext.Environment;

import mdm.com.LocalConfig;

/**
 * 连接SAP接口
 * @author YangChao
 *
 */
public class RfcManager {
    
    private static Logger logger = Logger.getLogger(RfcManager.class);

    private static String ABAP_AS_POOLED = ""; // 接口名

    private static JCOProvider provider = null;

    private static JCoDestination destination = null;

    private static Properties properties = null;

    private static LocalConfig localConfig = null;
    private static String      JCO_ASHOST;
    private static String      JCO_SYSNR;
    private static String      JCO_USER;
    private static String      JCO_PASSWD;
    private static String      JCO_CLIENT;
    private static String      JCO_LANG;
    private static String      JCO_POOL_CAPACITY;
    private static String      JCO_PEAK_LIMIT;

    static {
        if (localConfig == null) {
            AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(LocalConfig.class);
            localConfig = (LocalConfig) context.getBean(LocalConfig.class);
        }
        //453测试接口
        JCO_ASHOST = localConfig.getJCO_ASHOST();
        JCO_SYSNR = localConfig.getJCO_SYSNR();
        JCO_USER = localConfig.getJCO_USER();
        JCO_PASSWD = localConfig.getJCO_PASSWD();
        JCO_CLIENT = localConfig.getJCO_CLIENT();
        JCO_LANG = localConfig.getJCO_LANG_zh();
//        JCO_LANG = localConfig.getJCO_LANG_vi();
        JCO_POOL_CAPACITY = localConfig.getJCO_POOL_CAPACITY();
        JCO_PEAK_LIMIT = localConfig.getJCO_PEAK_LIMIT();
        
        properties = loadProperties();

        provider = new JCOProvider();

        // catch IllegalStateException if an instance is already registered
        try {
            Environment.registerDestinationDataProvider(provider);
        } catch (IllegalStateException e) {
            logger.debug(e);
        }

    }

    public static Properties loadProperties() {
        Properties prop = new Properties();
        prop.setProperty(DestinationDataProvider.JCO_ASHOST, JCO_ASHOST); // 服务器
        prop.setProperty(DestinationDataProvider.JCO_SYSNR, JCO_SYSNR); // 系统编号
        prop.setProperty(DestinationDataProvider.JCO_USER, JCO_USER); // SAP 用户名
        prop.setProperty(DestinationDataProvider.JCO_PASSWD, JCO_PASSWD); // SAP密码
        prop.setProperty(DestinationDataProvider.JCO_CLIENT, JCO_CLIENT); // SAP集团
        prop.setProperty(DestinationDataProvider.JCO_LANG, JCO_LANG); // 登录语音
        prop.setProperty(DestinationDataProvider.JCO_POOL_CAPACITY, JCO_POOL_CAPACITY); // 最大连接数
        prop.setProperty(DestinationDataProvider.JCO_PEAK_LIMIT, JCO_PEAK_LIMIT); // 最大连接线程

        return prop;
    }

    public static JCoDestination getDestination() throws JCoException {
        if (destination == null) {
            destination = JCoDestinationManager.getDestination(ABAP_AS_POOLED);
        }
        return destination;
    }

    public static JCoFunction getFunction(String functionName) {
        ABAP_AS_POOLED = functionName;
        provider.changePropertiesForABAP_AS(functionName, properties);
        JCoFunction function = null;
        try {
            function = getDestination().getRepository().getFunctionTemplate(functionName).getFunction();
        } catch (JCoException e) {
            logger.error(e);
        } catch (NullPointerException e) {
            logger.error(e);
        }
        return function;
    }

    public static void execute(JCoFunction function) {
        logger.debug("SAP Function Name : " + function.getName());
        JCoParameterList paramList = function.getImportParameterList();

        if (paramList != null) {
            logger.debug("Function Import Structure : " + paramList.toString());
        }

        try {
            function.execute(getDestination());
        } catch (JCoException e) {
            logger.error(e);
        }
        paramList = function.getExportParameterList();

        if (paramList != null) {
            logger.debug("Function Export Structure : " + paramList.toString());
        }
    }

    /*
     * SAP Ping
     */
    public static String ping() {
        String msg = null;
        try {

            getDestination().ping();
            msg = "Destination " + ABAP_AS_POOLED + " is ok";
        } catch (JCoException ex) {
            logger.debug(ex);
        }
        logger.debug(msg);
        return msg;
    }

}
